<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("./_inc/crulFunction.php");

$errMsg = "";
$module = sxREQ('mod');
$name = sxREQ('name');
$alias = sxREQ('alias');

if($module=='' || $name=='') {
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("missing name '$module/$name'"));
	serverRedir($url);
}

if(sxREQ("reqtype") == "save"){
	$errMsg .= crulAliasMod();
}
$sSQL = "SELECT * FROM com_rule_alias "
	." WHERE module='".sxRAS('mod')."' AND name='".sxRAS('name')."' "
	." AND com_alias='".sxRAS('alias')."'";
$objSDAO->query($sSQL);
if(!$row = $objSDAO->fetch()){
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("missing alias '$module/$name/$alias'"));
	serverRedir($url);
}
sxRDef('description',$row['description']);
sxRDef('entity',$row['entity']);
sxRDef('read_only',$row['read_only']);
sxRDef('note',$row['note']);

$sSQL = "SELECT * FROM entity ORDER BY entity";
$objSDAO->query($sSQL);
$aEntity = array();
while($row = $objSDAO->fetch()){
	$aEntity[$row['entity']] = $row['entity'];
}

$aOpz = array("kMenu"=>'pro.cru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>
<?php
echo(bodyHTML($aOpz));

echo getCrulHeader($module,$name);

$aCmd = array("save"=>array("pag"=>"javascript:objFrmSubm('save')",
										"title"=>_t("save"),
										"text"=>_t("save")
										),
					"back"=>array()
					);
$sCmd = commandsHTML($aCmd);	
echo $sCmd;

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','...');
echo getHtmlHidden('mod');
echo getHtmlHidden('name');
echo getHtmlHidden('alias');

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>Alias</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Alias</td>" .
			"<td class=\"text01\">");
echo $alias;	
echo("</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Entity*</td>" .
			"<td class=\"text01\">");
echo getHtmlSelect('entity', $aEntity) .
		"</tr>\n";
echo("<tr>" .
			"<td class=\"labl01\">Description</td>" .
			"<td class=\"text01\">".
			getHtmlText('description',80,80)."</td>" .
		"</tr>\n");
sxRdef('read_only','N');
echo("<tr>" .
			"<td class=\"labl01\">Read-only</td>" .
			"<td class=\"text01\">".
			getHtmlRadio("read_only",array('N'=>'No','Y'=>'Yes'))."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\">".
			getHtmlTextArea("note",80,3)."</td>" .
		"</tr>\n");
echo("</table>");

echo $sCmd;

echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function crulAliasMod(){
 	global $objSDAO;
	$errMsg = "";

	if($errMsg != ""){
		return $errMsg;
	}		
	$sSQL = "UPDATE com_rule_alias SET description = '".sxRAS("description")."' " .
				", entity = '".sxRAS("entity")."' " .
				", read_only = '".sxRAS("read_only")."' " .
				",note = '".sxRAS("note")."' " .
				" WHERE module = '".sxRAS("mod")."' " .
				" AND name = '".sxRAS("name")."' " .
				" AND com_alias = '".sxRAS("alias")."'";			
	$objSDAO->exec($sSQL);
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return 'redir err';
}
